package tv.youxing.android.ui;
/**
 * Created by apple on 2017/4/24.
 */

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import tv.youxing.android.R;
import tv.youxing.android.adapter.MyCollectionAdapter;
import tv.youxing.android.entity.MyCollectionModel;
import tv.youxing.android.entity.VideoPlayerPostModel;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

/**
 * 项目名：   youxing
 * 包名：     tv.youxing.android .ui
 * 文件名：   BatchDownloadActivity
 * 创建者：   叶克武
 * 创建时间： 2017/4/24 上午9:59
 * 描述：    TODO
 */
public class BatchDownloadActivity extends BaseActivity {


    @BindView(R.id.gv)
    GridView mGridView;

    MyCollectionModel allData;
    List<MyCollectionModel.ResultBean.DataBean> listDatas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_batch_download);
        ButterKnife.bind(this);

        loadData();
    }

    private void loadData() {
        //post请求简洁版实现
        HttpParams params = new HttpParams();
        params.putHeaders("Authorization","Bearer "+ ShareUtils.getString(this, AppConst.TOKEN,""));

        RxVolley.post(StaticClass.myFabulous, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                allData = GsonUtils.GsonToBean(t,MyCollectionModel.class);

                if (allData.getCode() == AppConst.TOKENERROR) {
                    BatchDownloadActivity.this.startActivity(new Intent(BatchDownloadActivity.this,LoginActivity.class));
                    return;
                }

                listDatas = allData.getResult().getData();
                mGridView.setAdapter(new MyCollectionAdapter(BatchDownloadActivity.this,listDatas));
                mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        MyCollectionModel.ResultBean.DataBean m = listDatas.get(i);
                        VideoPlayerPostModel model = new VideoPlayerPostModel();
                        model.setMultimedia_id(m.getMultimedia_id());
                        model.setTitle(m.getTitle());
                        model.setIntro(m.getIntro());
                        model.setCover(m.getCover());
                        model.setDuration(m.getDuration());
                        model.setPublish_at(m.getPublish_at());
                        model.setPlay_url(m.getPlay_url());
                        model.setType(m.getType());
                        model.setPlay_count(m.getPlay_count());
                        model.setComment_count(m.getComment_count());
                        model.setLike_count(m.getLike_count());
                        model.setCollection_count(m.getCollection_count());
                        model.setStatus(m.getStatus());
                        model.setCreated_at(m.getCreated_at());
                        model.setUpdated_at(m.getUpdated_at());
                        model.setAlbum_name(m.getAlbum_name());
                        VideoPlayerPostModel.AlbumBean albumBean = new VideoPlayerPostModel.AlbumBean();
                        albumBean.setAlbum_id(m.getAlbum().getAlbum_id());
                        albumBean.setName(m.getAlbum().getName());
                        albumBean.setCover(m.getAlbum().getCover());
                        albumBean.setIs_order(m.getAlbum().getIs_order());
                        model.setAlbum(albumBean);

                        Intent intent = new Intent(BatchDownloadActivity.this, SubscribeDeteilsActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putSerializable("data",model);
                        intent.putExtras(bundle);
                        BatchDownloadActivity.this.startActivity(intent);
                    }
                });
            }
        });
    }

}
